/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds.object;

import java.awt.Cursor;
import net.worlds.WorldsException;
import net.worlds.Main;
import net.worlds.lua.Event;
import net.worlds.ui.UI;
import org.jbox2d.collision.shapes.Shape;
import org.jbox2d.common.Vec2D;


public class DragTool extends Tool{
    Part lastPart;
    public DragTool() throws WorldsException
    {
        super("DragTool");
        addEvent("DragStarted", new Event(), "dragPart (GameObject)");
    }
    @Override
    public void selected()
    {
        if(!Main.noView)
        {
            UI.mouseMode = UI.MODE_ARROW;
            UI.setCursor(Cursor.getDefaultCursor());
            UI.setMouseImage(null);
        }
    }
    @Override
    public void mouseDown(int b, Vec2D pos)
    {
        super.mouseDown(b, pos);
        if(b == 1)
        {
            try {
                lastPart = ((World) world.getService("World")).getPartAt(pos);
                if(lastPart != null)
                {
                    for (Shape s = lastPart.getBody().getShapeList(); s != null; s = s.m_next)
                    {
                        lastPart.getBody().getShapeList().m_isSensor = true;
                    }
                    getEvent("DragStarted").fire(new Object[]{lastPart});
                }
            } catch (WorldsException ex) {
                ex.printStackTrace();
            }
        }
        if(b == 3)
        {
            try {
                Part p = ((World) world.getService("World")).getPartAt(pos);
                if(p == null)
                    return;
                if (p.getBooleanProp("Fixed") == true) {
                    p.setProp("Fixed", false, true);
                } else {
                    p.setProp("Fixed", true, true);
                }
            } catch (WorldsException ex) {
                throw new RuntimeException(ex);
            }
        }
    }
    @Override
    public void mouseUp(int b, Vec2D pos)
    {
        if(lastPart != null)
        {
                    for (Shape s = lastPart.getBody().getShapeList(); s != null; s = s.m_next)
                    {
                        lastPart.getBody().getShapeList().m_isSensor = false;
                    }
        }
        lastPart = null;
    }
    @Override
    public void deselected()
    {
        try {
            if(!Main.noView)
                UI.clearSelection(false);
        } catch (WorldsException ex) {
            ex.printStackTrace();
        }
        UI.clearMouseMode();
    }
}

/*
link(script.Parent.Selected, function() print("S") end)
link(script.Parent.Deselected, function() print("d") end)
link(script.Parent.MouseDown, function(b, p) print(b); print(p); end)
link(script.Parent.MouseUp, function(p) print(p) end)
 */